package com.eascs.partner.dao;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.brand.form.O2oBranPage;
import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;
import com.eascs.partner.entity.DealerPartnerEntity;
import com.eascs.partner.form.PartnerQueryForm;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;

@Repository
public class JxsPartnerDao extends BaseO2oDao {
	private static final String table_name="o2o_dealer_partner_info";
	public static final byte status_delete=2;
	public static final byte status_normal=1;
	public static final byte wfstatus_new=-1;
	
	public boolean deleteJxsPartnerEntityById(String partnerId){
		String sql="UPDATE " + table_name + " SET STATUS=? WHERE id=?";
		
		int result=o2oJdbcSession.execute(sql, new Object[]{status_delete,partnerId});
		return result > 0 ? true : false;
	}
	
	public DealerPartnerEntity queryJxsPartnerEntityById(String partnerId){
		String sql="SELECT * FROM  o2o_dealer_partner_info  WHERE id=?";
		
		return o2oJdbcSession.findEntity(sql, DealerPartnerEntity.class, partnerId);
	}
	
	public boolean processJxsPartnerStart(String partnerId){
		String sql="UPDATE o2o_dealer_partner_info SET wf_status=? WHERE id=?";
		int result=o2oJdbcSession.execute(sql, new Object[]{ProcessTaskMain.PROECESS_STATUS_HANDING,partnerId});
		return result > 0 ? true : false;
	}
	
	public boolean updateJxsPartnerWfStatus(String partnerId,int wfStatus){
		String sql="UPDATE o2o_dealer_partner_info SET wf_status=? WHERE id=?";
		int result=o2oJdbcSession.execute(sql, new Object[]{wfStatus,partnerId});
		return result > 0 ? true : false;
	}
	
	public boolean insertJsxPartner(DealerPartnerEntity entity){
		int result=o2oJdbcSession.insert(entity);
		return result > 0 ? true : false;
	}
	
	public boolean updateJsxPartner(DealerPartnerEntity entity){
		int result=o2oJdbcSession.updateById(entity);
		return result > 0 ? true : false;
	}
	
	public DealerPartnerEntity queryJxsPartnerEntityByName(String partnerName){
		String sql="SELECT * FROM  o2o_dealer_partner_info  WHERE business_license_name=?";
		
		return o2oJdbcSession.findEntity(sql, DealerPartnerEntity.class, partnerName);
	}
	
	
	public PageResult<Object[]> queryPartnerEntityByPage(O2oBranPage branForm) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, branForm.getPageSize(), branForm.getPageIndex());
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT p.id,p.pattern_code,p.business_license_name,p.create_date,p.operator,p.batch_classification,p.financing_limit,p.wf_status,p.loan_id ");
		sb.append(" FROM "+table_name);
		sb.append(" p WHERE p.brand_id=? and p.status=?");
		sb.append(" ORDER BY p.create_date DESC ");
		sqlPageQuery.addParam(branForm.getBrandId());
		sqlPageQuery.addParam(status_normal);
		sqlPageQuery.setQuerySql(sb.toString());
		return o2oJdbcSession.findDataWithPage(sqlPageQuery);
	}
	
	
	public String getProvinceByLoan(String loanNo){
		String sql ="SELECT r.province_code from o2o_loanapply t LEFT JOIN o2o_platform_regional r on t.ltd = r.platform_id where loanapply_no = ?";
		return o2oJdbcSession.findString(sql, loanNo);
	}
	
	
	public PageResult<Object[]> findPartnerInfoByPage(PartnerQueryForm params, int pageIndex, int pageSize) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<Object[]>(Object[].class, pageSize, pageIndex);
		StringBuffer querySql = new StringBuffer();//查询语句拼接
		querySql.append(" SELECT p.id,p.pattern_code,p.operator,p.org_province,p.business_license_name,p.batch_classification,b.BRAND_NAME,p.financing_limit,DATE_FORMAT(p.create_date , '%Y-%m-%d'),p.wf_status,p.loan_id ");
		querySql.append(" FROM "+table_name+" p ");
		querySql.append(" LEFT JOIN O2O_BRAND_INFO b ON p.BRAND_ID = b.ID");
		querySql.append(" WHERE /**DATA_PERMISSION(p.org_id)**/ 1=1 ");
		//按传入条件参数查询相关数据
		if (StringUtils.isNotBlank(params.getPartnerName())) {
			querySql.append(" AND P.business_license_name LIKE ? ");
			sqlPageQuery.addLikeParam(params.getPartnerName());
		}
		if (StringUtils.isNotBlank(params.getBrandName())) {
			querySql.append(" AND B.BRAND_NAME LIKE ? ");
			sqlPageQuery.addLikeParam(params.getBrandName());
		}
		
		if (!StringUtil.isEmpty(params.getCreateTime())) {
			querySql.append(" AND P.create_date >= ? ");
			sqlPageQuery.addParam(params.getCreateTime());
		}
		
		if (!StringUtil.isEmpty(params.getCreateEndTime())) {
			querySql.append(" AND P.create_date < ?");
			sqlPageQuery.addParam(DateUtils.formateStandDate(DateUtils.addDate(DateUtils.formateStandDate(params.getCreateEndTime()), 1)));
		}
		if (StringUtils.isNotBlank(params.getHostProvince())) {
			querySql.append(" AND P.ORG_PROVINCE_ID= ? ");
			sqlPageQuery.addParam(params.getHostProvince());
		}
		if (StringUtils.isNotBlank(params.getStatus())) {
			querySql.append(" AND P.WF_STATUS= ? ");
			sqlPageQuery.addParam(params.getStatus());
		}
		if(StringUtils.isNotBlank(params.getOperator())){
			querySql.append(" AND P.operator_id= ? ");
			sqlPageQuery.addParam(params.getOperator());
		}
		querySql.append(" AND p.STATUS = ? ");
		sqlPageQuery.addParam(status_normal);
		querySql.append(" ORDER BY P.create_date DESC");
		sqlPageQuery.setQuerySql(querySql.toString());
		return o2oJdbcSession.findDataWithPage(sqlPageQuery);
	}
}

